package com.aivin.tvslidebar.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.aivin.tvslidebar.R;
import com.aivin.tvslidebar.v4.AbstractWheel;
import com.aivin.tvslidebar.v4.OnWheelChangedListener;
import com.aivin.tvslidebar.v4.OnWheelScrollListener;
import com.aivin.tvslidebar.v4.WheelAdapter;
import com.aivin.tvslidebar.v4.WheelHorizontalView;


public class ScrollPickerV4Activity extends Activity  implements OnWheelScrollListener, OnWheelChangedListener {

    private WheelHorizontalView shutterWheel;
    private ShutterWheelAdapter adapterShutter;
    private String[] shutterNameArray;
    private int curShutterPos = 0;
    private boolean wheelScrolling;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scrollpicker_v4);
        init();
        updateShutterWheel();

    }

    private void init() {
        findViewById(R.id.btnTest1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shutterWheel.setCurrentItem(5);
            }
        });

        findViewById(R.id.btnTest2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                shutterWheel.setCurrentItem(7);
            }
        });

        shutterWheel = findViewById(R.id.wheelview_camera_settings_shutter);
        shutterNameArray = this.getResources().getStringArray(R.array.camera_shutter_names);
    }

    private void updateShutterWheel() {
        adapterShutter = new ShutterWheelAdapter( this, shutterNameArray);
        adapterShutter.setItemResource(R.layout.wheel_item_camera_set_shutter);
        adapterShutter.setItemTextResource(R.id.camera_settings_wheel_text);
        adapterShutter.setCurPos(curShutterPos);

        shutterWheel.removeChangingListener(this);
        shutterWheel.addChangingListener(this);
        shutterWheel.removeScrollingListener(this);
        shutterWheel.addScrollingListener(this);

        shutterWheel.setViewAdapter(adapterShutter);
        shutterWheel.setCurrentItem(curShutterPos);
    }

    private class ShutterWheelAdapter extends WheelAdapter<String> {

        public ShutterWheelAdapter(Context context, String[] items) {
            super(context, items);
        }

        @Override
        protected CharSequence getItemText(int index) {
            String text = (String) super.getItemText(index);
            if (!(ScrollPickerV4Activity.this.shutterWheel.isEnabled())) {
                if (!text.contains("\"")) {
                    text = "1/" + text + "\"";
                }
            }

            return text;
        }
    }


    @Override
    public void onScrollingStarted(AbstractWheel wheel) {
        wheelScrolling = true;
    }

    @Override
    public void onScrollingFinished(final AbstractWheel wheel) {
        wheelScrolling = false;
        int position = wheel.getCurrentItem() ;
        curShutterPos = position;
        final String newShutter =  shutterNameArray[curShutterPos];
        LyLog.showLog("当前选中==="+ position+"   "+newShutter);
    }

    @Override
    public void onChanged(AbstractWheel wheel, int oldValue, int newValue) {
        if (wheelScrolling) {
            adapterShutter.setCurPos(newValue);
            shutterWheel.setCurrentItem(newValue);
        }
    }

}
